# This Python file uses the following encoding: utf-8
############################################################################
# Copyright (c) 2025 Li Auto Inc. and its affiliates
# Licensed under the Apache License, Version 2.0(the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
############################################################################
from enum import Enum, unique
from dataclasses import dataclass, field


class TraceEventStr(Enum):
    SCHED_WAKEUP_STR = "sched_wakeup"
    SCHED_SWITCH_STR = "sched_switch"
    IRQ_ENTRY_STR = "irq_handler_entry"
    IRQ_EXIT_STR = "irq_handler_exit"
    MARK_WRITE = "tracing_mark_write"


@unique
class IrqState(Enum):
    IRQ_HANDLED = 0
    IRQ_UNHANDLED = 1


IRQ_STATE = {IrqState.IRQ_HANDLED.value: "handled", IrqState.IRQ_UNHANDLED.value: "unhandled"}


@dataclass
class TaskData:
    """task数据"""
    name: str = ""
    task_id: int=  -1
    priority: int = -1  # 表示task优先级
    core_id: int = -1

    ready_state: bool = False  # 是否ready
    last_ready_time: float = 0  # 上一次进ready的时间
    ready_period_total: float = 0   # 相邻两次ready的周期的累加值
    ready_cnt: int = 0              # 进ready的总次数-1

    period_max: float = 0  # 所有实际周期的最大值.Maximum period of the task
    period_min: float = 0  # 所有实际周期的最小值.Minimum period of the task
    period_total: float = 0  # 所有实际周期的累加值.Total period of the task
    period_count: int = 0  # 执行周期的次数.Total period time

    execute_time_cal: float = 0  # CET执行时间,表示在running状态的实际时间.Execute time of the task
    execute_time_max: float = 0  # 所有实际执行时间的最大值.Maximum execute time of the task
    execute_time_min: float = 0  # 所有实际执行时间的最小值.Minimum execute time of the task
    execute_time_total: float = 0  # 总共的执行时间.Total execute time of the task
    execute_count: int = 0  # 执行次数,即从ready进入running的次数.Total execute time

    irq_time: float = 0  # task执行期间irq运行的时间

    get_total: float = 0  # 总共的GET执行时间,表示task从ready切入running的时间,至task切出running且离开ready的时间差
    get_max: float = 0
    get_min: float = 0

    last_start_time: float = 0  # 上一次从ready进入running的时间.Time stamp of the last active time
    last_end_time: float = 0  # 上一次退出running的时间.
    last_execute_time: float = 0  # 最近一次进入running的时间.Time stamp of the last execute time

@dataclass
class IrqData:
    """ISR数据"""
    name: str = ""

    period_max: float = 0  # 所有实际周期的最大值.Maximum period of the ISR
    period_min: float = 0  # 所有实际周期的最小值.Minimum period of the ISR
    period_total: float = 0  # 所有实际周期的累加值.Total period of the ISR
    period_count: int = 0  # 执行周期的次数.Total period time

    execute_time_max: float = 0  # 所有实际执行时间的最大值.Maximum execute time of the ISR
    execute_time_min: float = 0  # 所有实际执行时间的最小值.Minimum execute time of the ISR
    execute_time_total: float = 0  # 总共的执行时间.Total execute time of the ISR
    execute_count: int = 0  # 执行次数,即进入ISR并退出的次数.Total execute time

    get_total: float = 0    # 整体执行时间
    get_max: float = 0
    get_min: float = 0

    preemption_time: float = 0  # ISR执行期间被其他ISR抢占的时间

    last_execute_time: float = 0  # 最近一次进入running的时间.Time stamp of the last execute time


@dataclass
class EventData:
    """event对象的trace数据"""
    name: str = ""
    start_time: float = 0  # 首次开始时间
    last_time: float = 0  # 上次开始时间

    period_time: int = 0  # 理论周期(ms)
    period_total: float = 0  # 所有实际周期的累加值
    period_count: int = 0  # 执行周期的次数

    jitter_max: float = 0  # 周期抖动的最大值
    jitter_total: float = 0  # 所有抖动的累加值


@dataclass
class RunnableData:
    """runnable对象的trace数据"""
    name: str = ""
    task_id_list: list = field(default_factory=list)    # 所属task id

    start_time: float = 0  # 首次开始时间
    last_time: float = 0  # 上次开始时间

    period_time: int = 0  # 理论周期(ms)
    period_total: float = 0  # 所有实际周期的累加值
    period_count: int = 0  # 执行周期的次数

    jitter_max: float = 0  # 周期抖动的最大值
    jitter_total: float = 0  # 所有抖动的累加值


@dataclass
class FunctionData:
    """function对象的trace数据"""
    name: str = ""
    start_time: float = 0  # 首次开始时间
    last_time: float = 0  # 上次开始时间

    period_time: int = 0  # 理论周期(ms)
    period_total: float = 0  # 所有实际周期的累加值
    period_count: int = 0  # 执行周期的次数

    jitter_max: float = 0  # 周期抖动的最大值
    jitter_total: float = 0  # 所有抖动的累加值


@dataclass
class CoreData:
    """单个core的实时数据"""
    idle_task_id: int = -1  # 内核中idle task的id
    idle_time: float = 0  # idle task总共的CET
    irq_in_idle_time: float = 0  # irq在idle_time中总共的执行时间
    total_time: float = 0
    last_time_stamp: float = 0
    task_id_list: list = field(default_factory=list)
    irq_id_list: list = field(default_factory=list)
